package com.atguigu.flink.chapter07;

import com.atguigu.flink.bean.WaterSensor;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/12/15 9:26
 */
public class Flink06_Window_ProcessFunction_2 {
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        env
            .socketTextStream("hadoop162", 9999)
            .map(line -> {
                String[] data = line.split(",");
                return new WaterSensor(data[0], Long.valueOf(data[1]), Integer.valueOf(data[2]));
            })
            .keyBy(WaterSensor::getId)
            .window(TumblingProcessingTimeWindows.of(Time.seconds(5)))
            .aggregate(
                new AggregateFunction<WaterSensor, MyAvg, Double>() {
                    // 初始化一个累加器
                    @Override
                    public MyAvg createAccumulator() {
                        System.out.println("Flink06_Window_ProcessFunction_2.createAccumulator");
                        return new MyAvg();
                    }
    
                    // 累加
                    @Override
                    public MyAvg add(WaterSensor value, MyAvg acc) {
                        System.out.println("Flink06_Window_ProcessFunction_2.add");
                        acc.sum += value.getVc();
                        acc.count++;
        
                        return acc;
                    }
    
                    // 返回最终的聚合结果
                    @Override
                    public Double getResult(MyAvg acc) {
                        System.out.println("Flink06_Window_ProcessFunction_2.getResult");
                        return acc.avg();
                    }
    
                    // 合并两个累加器
                    // 这个方法只有在session窗口才会触发, 其他窗口不会触发, 可以不用实现
                    @Override
                    public MyAvg merge(MyAvg a, MyAvg b) {
                        System.out.println("Flink06_Window_ProcessFunction_2.merge");
                        return null;
        
                    }
                },
                new ProcessWindowFunction<Double, String, String, TimeWindow>() {
                    @Override
                    public void process(String key,
                                        Context ctx,
                                        Iterable<Double> elements,
                                        Collector<String> out) throws Exception {
                        System.out.println(key + "  " + ctx.window() + " " + elements.iterator().next());
                    }
                }
            )
            .print();
        
        env.execute();
    }
    
    public static class MyAvg {
        public Integer sum = 0;
        public Long count = 0L;
        
        public Double avg() {
            return sum * 1.0 / count;
        }
    }
}
/*
窗口处理函数:

增量
    sum max min maxBy minBy

    reduce
    
    aggregate
    
全量
    process
 */